package p3.structures;

import java.util.Random;

import p3.structures.TreeNode.Alphabet;

public class TreeNode {

	protected Alphabet term;
	
	public TreeNode(){}
	
	public TreeNode(TreeNode node){
		term = node.getTerm();
	}
	
	public String toString(){ return term.toString(); }
	public int getArity(){ return term.getArity(); }
	
	public void setRandom(){
		Random r = new Random();
		if (r.nextBoolean())
			setRandomTerminal();
		else
			setRandomFunction();
	}
	
	public void setRandomTerminal(){ 
		Random r = new Random();
		switch (r.nextInt(5)){
		case 0: term = Terminal.LEFT; break;
		case 1: term = Terminal.RIGHT; break;
		case 2: term = Terminal.FIRE; break;
		case 3: term = Terminal.DIST_X; break;
		case 4: term = Terminal.DIST_Y; break;
		}
	}
	
	public void setRandomFunction(){
		Random r = new Random();
		switch (r.nextInt(4)){
		case 0: term = Function.IF; break;
		case 1: term = Function.EQ; break;
		case 2: term = Function.PROGN2; break;
		case 3: term = Function.PROGN3; break;
		}
	}
	
	
	public interface Alphabet{ public int getArity(); }
	
	
	public enum Terminal implements Alphabet{
		
		LEFT{ public String toString() { return "IZQUIERDA";} 	public int getArity(){return 0;}}, 
		RIGHT{ public String toString() { return "DERECHA";} 	public int getArity(){return 0;}}, 
		FIRE{ public String toString() { return "FIRE";} 		public int getArity(){return 0;}}, 
		DIST_X{ public String toString() { return "DIST_X";} 	public int getArity(){return 0;}}, 
		DIST_Y{ public String toString() { return "DIST_Y";} 	public int getArity(){return 0;}}
	}
	
	public enum Function implements Alphabet{
		IF{ public String toString() { return "if";} 			public int getArity(){return 3;}}, 
		EQ{ public String toString() { return "eq";}			public int getArity(){return 2;}}, 
		PROGN2{ public String toString() { return "progn2";} 	public int getArity(){return 2;}}, 
		PROGN3{ public String toString() { return "progn3";} 	public int getArity(){return 3;}}
	}
	
	public static void main(String[] args){
		TreeNode term = new TreeNode();

		term.setRandomFunction();
		System.out.println(term);
		term.setRandomTerminal();
		System.out.println(term);
	}
	public Alphabet getTerm() { return term;}
	
	public void setDifferentTerminal(){
		String oldTerm = toString();
		setRandomTerminal();
		while (oldTerm.equals(term.toString())) setRandomTerminal();
	}
	
	public void setDifferentFunction(){
		if (term.getArity() == 2){
			if (term.toString().equals("eq")){
				term = Function.PROGN2;
			} else if (term.toString().equals("progn2")){
				term = Function.EQ;
			}
		} else if (term.getArity() == 3){
			if (term.toString().equals("if")){
				term = Function.PROGN3;
			} else if (term.toString().equals("progn3")){
				term = Function.IF;
			}
		}
	}
	public TreeNode getCopy() { return new TreeNode(this); }
}
